<?php
class student extends control
{
    public function __construct()
    {
        parent::__construct();
        $this->app->loadLang('index');
    }
    public function index($recTotal = 0, $recPerPage = 20, $pageID = 0)
    {
        $this->app->loadClass('pager');
        $pager = new pager($recTotal, $recPerPage, $pageID);
        $this->view->title    = $this->lang->student->index;
        $this->view->students = $this->student->getList($pager);
        $this->view->pager    = $pager;
        $this->display();
    }
    /**
     * Create an article.
     *
     * @access public
     * @return void
     */
    public function create()
    {
        if(!empty($_POST))
        {
            $studentId = $this->student->create();
            if(dao::isError()) die(js::error(dao::getError()) . js::locate('back'));
            die(js::locate(inlink('index')));
        }
        // 获取校区列表
        $this->view->campusList = $this->student->lsCampus();
        $this->display();
    }

    /**
     * Update an article.
     *
     * @param  int    $id
     * @access public
     * @return void
     */
    public function edit($id)
    {
        if(!empty($_POST)) {
            $this->student->update($id);
            $this->locate(inlink('index'));
        }
        else {
            $this->view->student = $this->student->getByID($id);
            $this->view->campusList = $this->student->lsCampus();
            $this->display();
        }
    }
    public function view($id)
    {
        $this->view->student = $this->student->getByID($id);
          if($this->view->student->sex == 0){
              $this->view->student->sex = "男";
          }else if ($this->view->student->sex == 1) {
              $this->view->student->sex = "女";
          } else {
              $this->view->student->sex = "未知";
          }
        $this->display();
    }

    /**
     * Delete an article.
     *
     * @param  int    $id
     * @access public
     * @return void
     */
    public function delete($id)
    {
        $this->student->delete($id);
        $this->locate(inlink('index'));
    }
    public function charge($studentId)
    {
        if(!empty($_POST)) {
            $this->student->charge();
            $this->locate(inlink('index'));
        }
        else {
            $this->view->accountClassList = $this->student->lsAccountClass();
            $this->view->account = $this->student->getaccount($studentId);
            $this->display();
        }
    }
    public function export()
    {
        // 导出用户表-->全部导出
        include_once "../../lib/PHPExcel/PHPExcel.php";
        $students = $this->student->getList();
        $objPHPExcel = new \PHPExcel();
        // Set properties
        $objPHPExcel->getProperties()->setCreator("ctos")
            ->setLastModifiedBy("ctos")
            ->setTitle("Office 2007 XLSX Test Document")
            ->setSubject("Office 2007 XLSX Test Document")
            ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
            ->setKeywords("office 2007 openxml php")
            ->setCategory("Test result file");
        // set width
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
        // 字体和样式
        $objPHPExcel->getActiveSheet()->getStyle('A1:F1')->getFont()->setBold(true);
        //设置水平居中
        $objPHPExcel->getActiveSheet()->getStyle('A1:F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //$objPHPExcel->getActiveSheet()->mergeCells('A1:D1');//  合并
        $objPHPExcel->setActiveSheetIndex(0) // 表头
        ->setCellValue('A1','序号')
            ->setCellValue('B1', '校区')
            ->setCellValue('C1', '学生')
            ->setCellValue('D1', '性别')
            ->setCellValue('E1', '学校/年级/班级')
            ->setCellValue('F1', '日期');
        // 内容
        foreach ($students as $key => $val) {
            if($key->sex == 0){
                $sex = "男";
            }else if ($key->sex == 1) {
                $sex = "女";
            } else {
                $sex = "未知";
            }
            $objPHPExcel->getActiveSheet(0)->setCellValue('A' . ($key + 2),$val->id);
            $objPHPExcel->getActiveSheet(0)->setCellValue('B' . ($key + 2), $val->campus);
            $objPHPExcel->getActiveSheet(0)->setCellValue('C' . ($key + 2), $val->student);
            $objPHPExcel->getActiveSheet(0)->setCellValue('D' . ($key + 2), $sex);
            $objPHPExcel->getActiveSheet(0)->setCellValue('E' . ($key + 2), $val->school.$val->class.$val->grade);
            $objPHPExcel->getActiveSheet(0)->setCellValue('F' . ($key + 2), $val->date);
        }
        // Rename sheet
        $objPHPExcel->getActiveSheet()->setTitle("学生列表");
        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);
        // 输出
        ob_end_clean();
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . "学生列表" . '.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }
}